package muview;

import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.cloudgarden.resource.SWTResourceManager;

import mumethod.*;
import muobject.*;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.springframework.context.ApplicationContext;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class TeacherPage extends Composite {
	
	private Label lab_title;
	private Label lab_titleschool;
	private Label lab_titleschoolname;
	private Label lab_titleyear;
	
	private static String APP_TITLE = "活力恆動一二三";
	private Combo cmb_year;

	private SystemProperty systemProperty;
	private ApplicationContext springConnector;
	private String rootPath;
	private static String imagePath;
	private String dbPath;
	private String currentYear;
	private School school;
	private String sno;
	private List<String> years;
	private AccessSchools getSchools;
	private AccessStudents getStudents;
	private CTabFolder tabFolder;
	private CTabItem one;
	private CTabItem two;
	private CTabItem three;
	private CTabItem four;
	private CTabItem five;
	
	public TeacherPage(Shell parent, int style, SystemProperty systemProperty) {
		super(parent, style);
		Init(parent, systemProperty);
	}

	public void Init(Shell parent, SystemProperty systemProperty) {
		System.setProperty("file.encoding", "big5");
		springConnector = systemProperty.getSpringConnector();
		rootPath = systemProperty.getRootPath();
		imagePath = systemProperty.getImagePath();
		dbPath = systemProperty.getDbPath();
		getSchools = (AccessSchools) springConnector.getBean("AccessSchoolsService");
		getStudents= (AccessStudents) springConnector.getBean("AccessStudentsService");
		currentYear = systemProperty.getCurrentYear();
		sno = systemProperty.getSno();
		systemProperty.setGrade("%");
		systemProperty.setSclass("%");
		createContents(parent, systemProperty);
	}

	private void createContents(final Shell parent, final SystemProperty systemProperty) {
		this.systemProperty = systemProperty;
		GridLayout thisLayout = new GridLayout();
		this.setLayout(thisLayout);
		{
			GridData mainPageCompositeGridData = new GridData();
			mainPageCompositeGridData.widthHint = 1005;
			mainPageCompositeGridData.heightHint = 80;
			GridLayout mainPageCompositeGridLayout = new GridLayout();
			mainPageCompositeGridLayout.numColumns = 10;
			mainPageCompositeGridLayout.makeColumnsEqualWidth = true;
			Composite mainPageComposite;
			mainPageComposite = new Composite(this, SWT.NONE);
			mainPageComposite.setLayout(mainPageCompositeGridLayout);
			mainPageComposite.setLayoutData(mainPageCompositeGridData);
			{
				GridData lab_titleGridData = new GridData(GridData.VERTICAL_ALIGN_END);
				lab_titleGridData.widthHint = 1005;
				lab_titleGridData.horizontalSpan = 10;
				lab_title = new Label(mainPageComposite, SWT.NONE);
				lab_title.setFont(SWTResourceManager.getFont("Tahoma", 24, 1, false, false));
				lab_title.setLayoutData(lab_titleGridData);
				lab_title.setAlignment(SWT.CENTER);
			}
			{
				GridData lab_titleschoolGridData = new GridData(GridData.VERTICAL_ALIGN_END);
				lab_titleschoolGridData.horizontalSpan = 1;
				lab_titleschoolGridData.horizontalAlignment = SWT.LEFT;
				lab_titleschool = new Label(mainPageComposite, SWT.NONE);
				lab_titleschool.setText("\u5b78\u6821\u540d\u7a31 : ");
				lab_titleschool.setFont(SWTResourceManager.getFont("Tahoma", 16, 1, false, false));
				lab_titleschool.setLayoutData(lab_titleschoolGridData);
				lab_titleschool.setForeground(new Color(mainPageComposite.getDisplay(), 255, 255, 255));
			}
			{
				school = getSchools.getTopClasses();
				GridData lab_titleschoolnameGridData = new GridData(GridData.VERTICAL_ALIGN_END);
				lab_titleschoolnameGridData.horizontalSpan = 5;
				lab_titleschoolnameGridData.horizontalAlignment = SWT.FILL;
				lab_titleschoolname = new Label(mainPageComposite, SWT.NONE);
				lab_titleschoolname.setText(school.getCname());
				lab_titleschoolname.setFont(SWTResourceManager.getFont("Tahoma", 16, 1, false, false));
				lab_titleschoolname.setLayoutData(lab_titleschoolnameGridData);
				lab_titleschoolname.setForeground(new Color(mainPageComposite.getDisplay(), 255, 255, 255));
			}
			{
				GridData lab_titleyearGridData = new GridData(GridData.VERTICAL_ALIGN_END);
				lab_titleyearGridData.horizontalSpan = 1;
				lab_titleyearGridData.horizontalAlignment = SWT.RIGHT;
				lab_titleyear = new Label(mainPageComposite, SWT.NONE);
				lab_titleyear.setText("\u5b78\u5e74:");
				lab_titleyear.setFont(SWTResourceManager.getFont("Tahoma", 16, 1, false, false));
				lab_titleyear.setLayoutData(lab_titleyearGridData);
				lab_titleyear.setForeground(new Color(mainPageComposite.getDisplay(), 255, 255, 255));
			}
			{
				years = getStudents.getAllYear(sno);
				GridData cmb_yearLData = new GridData(GridData.VERTICAL_ALIGN_END);
				cmb_yearLData.horizontalSpan = 2;
				cmb_yearLData.widthHint = 164;
				cmb_yearLData.heightHint = 21;
				cmb_year = new Combo(mainPageComposite, SWT.NONE);
				cmb_year.setLayoutData(cmb_yearLData);
				for(String str : years){
					if(!str.equals(String.valueOf(currentYear)))
					cmb_year.add(str + " - " + (Integer.valueOf(str) + 1));
				}
				cmb_year.add(currentYear + " - " + (Integer.valueOf(currentYear) + 1));
				cmb_year.select(cmb_year.getItemCount()-1);
				cmb_year.addSelectionListener(new SelectionAdapter() {
				      public void widgetSelected(SelectionEvent e) {
				       System.out.println(cmb_year.getText());
				       systemProperty.setYear(cmb_year.getText().substring(0,4));
				       systemProperty.setSno(school.getSno());
				       systemProperty.setGrade("%");
				       systemProperty.setSclass("%");
				       TabFolderLoad(parent, 0);
				       tabFolder.setSelection(tabFolder.getSelectionIndex());
				      }
				    });
			}

			GridData subPageCompositeGridData = new GridData();
			subPageCompositeGridData.widthHint = 995;
			subPageCompositeGridData.heightHint = 560;
			Composite subPageComposite;
			subPageComposite = new Composite(this, SWT.NONE);
			subPageComposite.setLayout(new FillLayout());
			subPageComposite.setLayoutData(subPageCompositeGridData);
			{
				tabFolder = new CTabFolder(subPageComposite,
						SWT.BORDER);
				
				CTabItem one = new CTabItem(tabFolder, SWT.NONE);
				one.setText("學校設定    ");
				one.setToolTipText("學校設定");
				one.setFont(SWTResourceManager.getFont("Tahoma", 12, 1, false,
						false));
				
				CTabItem two = new CTabItem(tabFolder, SWT.NONE);
				two.setText("輸入學生資料    ");
				two.setToolTipText("輸入學生資料");
				two.setFont(SWTResourceManager.getFont("Tahoma", 12, 1, false,
						false));
				
				CTabItem three = new CTabItem(tabFolder, SWT.NONE);
				three.setText("輸入學生測試記錄    ");
				three.setToolTipText("輸入學生測試記錄");
				three.setFont(SWTResourceManager.getFont("Tahoma", 12, 1,
						false, false));
				
				CTabItem four = new CTabItem(tabFolder, SWT.NONE);
				four.setText("輸出各資訊報表     ");
				four.setToolTipText("輸出各資訊報表 ");
				four.setFont(SWTResourceManager.getFont("Tahoma", 12, 1, false,
						false));
				
				
//				CTabItem five = new CTabItem(tabFolder, SWT.NONE);
//				five.setText("年終數據處理    ");
//				five.setToolTipText("年終數據處理");
//				five.setFont(SWTResourceManager.getFont("Tahoma", 12, 1, false,
//						false));
				
				TabFolderLoad(parent, 0);
				tabFolder.setSelection(0);
				tabFolder.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(
							org.eclipse.swt.events.SelectionEvent event) {
						System.out.println(tabFolder.getSelection().getText()
								+ " selected");
						if(tabFolder.getSelectionIndex() == 2 | tabFolder.getSelectionIndex() == 3){
							TabFolderLoad(parent, tabFolder.getSelectionIndex()+1);
						}
						tabFolder.setSelection(tabFolder.getSelection());
					}
				});
			}
		}
	}
	
	private void TabFolderLoad(Shell parent, int selectNo){
		if(selectNo == 1 | selectNo == 0){
			one = tabFolder.getItem(0);
			TeacherSchoolSetting teacherSchoolSetting = new TeacherSchoolSetting(tabFolder, parent, systemProperty, lab_titleschoolname);
			teacherSchoolSetting.setLayoutData(new FormLayout());
			teacherSchoolSetting.setBackground(new Color(tabFolder.getDisplay(), 255, 255, 255));
			one.setControl(teacherSchoolSetting);
		}
		
		if(selectNo == 2 | selectNo == 0){
			two = tabFolder.getItem(1);
			systemProperty.setSno(school.getSno());
			systemProperty.setYear(cmb_year.getText().substring(0,4));
			TeacherStudentInput teacherStudentInput = new TeacherStudentInput(tabFolder, parent, systemProperty);
			teacherStudentInput.setLayoutData(new FormLayout());
			teacherStudentInput.setBackground(new Color(tabFolder.getDisplay(), 255, 255, 255));
			two.setControl(teacherStudentInput);
		}
		if(selectNo == 3 | selectNo == 0){
			three = tabFolder.getItem(2);
			TeacherDataInput teacherDataInput = new TeacherDataInput(tabFolder, parent, systemProperty);
			teacherDataInput.setLayoutData(new FormLayout());
			teacherDataInput.setBackground(new Color(tabFolder.getDisplay(), 255, 255, 255));
			three.setControl(teacherDataInput);
		}
		if(selectNo == 4 | selectNo == 0){
			four = tabFolder.getItem(3);
			TeacherReport teacherReport  = new TeacherReport(tabFolder, parent, systemProperty);
			teacherReport.setLayoutData(new FormLayout());
			teacherReport.setBackground(new Color(tabFolder.getDisplay(), 255, 255, 255));
			four.setControl(teacherReport);
		}
		//five = tabFolder.getItem(4);
	}
	

	private Control getTabTwoControl(CTabFolder tabFolder) {
		// Create some labels and text fields
		Composite composite = new Composite(tabFolder, SWT.NONE);
		composite.setLayout(new RowLayout());
		new Label(composite, SWT.LEFT).setText("Label One:");
		new Text(composite, SWT.BORDER);
		new Label(composite, SWT.RIGHT).setText("Label Two:");
		new Text(composite, SWT.BORDER);
		return composite;
	}

	private Control getTabFourControl(CTabFolder tabFolder) {
		// Create some labels and text fields
		Composite composite = new Composite(tabFolder, SWT.NONE);
		composite.setLayout(new FillLayout());

		return composite;
	}

	public static void disply(SystemProperty systemProperty) {
		Display display = Display.getDefault();
		Shell shell = new Shell(display, SWT.SHELL_TRIM & (~SWT.RESIZE) & (~SWT.MAX));
		TeacherPage teacherPage = new TeacherPage(shell, SWT.None, systemProperty);
		GridLayout shellLayout = new GridLayout();
		shellLayout.makeColumnsEqualWidth = true;

		shell.setLayout(shellLayout);
		shell.setText("Complex Tabs");
		shell.setSize(1024, 700);
		shell.setText(APP_TITLE);
		shell.setImage(new Image(shell.getDisplay(), imagePath + "login.ico"));
		ImageData data = new ImageData(imagePath + "background.jpg");
		data.scaledTo(shell.getBounds().width, shell.getBounds().height);
		Image image = new Image(shell.getDisplay(), data);
		shell.setBackgroundMode(SWT.INHERIT_DEFAULT);
		shell.setBackgroundImage(image);
		
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		display.dispose();
	}

}